package test.administrator.example.com.mvptest1.framework.lce.impl.activity;

import android.view.View;

import test.administrator.example.com.mvptest1.R;
import test.administrator.example.com.mvptest1.framework.support.activity.MvpActivity;
import test.administrator.example.com.mvptest1.framework.MvpPresenter;
import test.administrator.example.com.mvptest1.framework.MvpView;
import test.administrator.example.com.mvptest1.framework.lce.ILceAnimator;
import test.administrator.example.com.mvptest1.framework.lce.MvpLceView;
import test.administrator.example.com.mvptest1.framework.lce.impl.MvpLceViewImpl;

/**
 * 代理模式->代理对象
 * Created by thinkpad on 2018/7/30.
 */

public abstract class MvpLceActivity<D, V extends MvpView, P extends MvpPresenter<V>> extends MvpActivity<V, P> implements MvpLceView<D> {

	private MvpLceViewImpl<D> mvpLceView;

	protected MvpLceViewImpl<D> getMvpLceView() {
		if (this.mvpLceView == null) {
			this.mvpLceView = new MvpLceViewImpl<D>();
		}
		return mvpLceView;
	}

	/**
	 * 加载LCE
	 */
	@Override
	public void onContentChanged() {
		super.onContentChanged();
		getMvpLceView().initView(findViewById(R.id.rootView));
		getMvpLceView().setOnClickErrorListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onErrorClick(v);
			}
		});
	}

	private void onErrorClick(View v) {
		//重新加载
		loadData(false);
	}

	public void setLceAnimator(ILceAnimator animator) {
		getMvpLceView().setAnimator(animator);
	}

	@Override
	public void loadData(boolean isPullRefresh) {
		getMvpLceView().loadData(isPullRefresh);
	}

	@Override
	public void showLoading(boolean isPullToRefresh) {
		getMvpLceView().showLoading(isPullToRefresh);
	}

	@Override
	public void showContent() {
		getMvpLceView().showContent();
	}

	@Override
	public void bindData(D data) {
		getMvpLceView().bindData(data);
	}

	@Override
	public void showError(Throwable e, boolean isPullToRefresh) {
		getMvpLceView().showError(e, isPullToRefresh);
	}

}
